﻿using System;
using System.Collections.Generic;
using System.IO;
using ProjScan.Modules;
using ProjScan.RecordManager.AssetData;
using ProjScan.ScanRule;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.RecordManager
{
	// Token: 0x02000284 RID: 644
	internal class RecordManagerBase<T> : IRecordManager where T : AssetDataBase
	{
		// Token: 0x0600110A RID: 4362 RVA: 0x000084AA File Offset: 0x000066AA
		public RecordManagerBase()
		{
			this.recordRules = new List<RecordRule>();
			this.dicEnableRules = new Dictionary<RecordRule, bool>();
		}

		// Token: 0x0600110B RID: 4363 RVA: 0x0004FC78 File Offset: 0x0004DE78
		public void AddRecordRule(RecordRule recordRule)
		{
			bool flag = this.recordRules == null;
			if (flag)
			{
				this.recordRules = new List<RecordRule>();
			}
			this.recordRules.Add(recordRule);
		}

		// Token: 0x0600110C RID: 4364 RVA: 0x000084CA File Offset: 0x000066CA
		public virtual void Close()
		{
			List<T> list = this.datas;
			if (list != null)
			{
				list.Clear();
			}
		}

		// Token: 0x0600110D RID: 4365 RVA: 0x0004FCAC File Offset: 0x0004DEAC
		public int ScanRecordCount()
		{
			bool flag = this.datas == null;
			int result;
			if (flag)
			{
				result = 0;
			}
			else
			{
				result = this.datas.Count;
			}
			return result;
		}

		// Token: 0x0600110E RID: 4366 RVA: 0x0004FCDC File Offset: 0x0004DEDC
		public void OutPutScanScr()
		{
			bool flag = this.datas != null && this.datas.Count != 0;
			if (flag)
			{
				string scanOutPut = ToolsHelper.GetScanOutPut();
				Debug.Log(string.Format("[{0}]Scan资源个数[{1}]", this.TableTag, this.datas.Count));
				StreamWriter streamWriter = Singleton<FileStreamFactory>.Instance.GetStreamWriter(scanOutPut + this.RecordFileName());
				streamWriter.WriteLine(this.datas[0].GetCSVHeader());
				for (int i = 0; i < this.datas.Count; i++)
				{
					string value = this.datas[i].GetInfo().Replace("\r", "").Replace("\n", "");
					streamWriter.WriteLine(value);
				}
				streamWriter.Close();
			}
		}

		// Token: 0x0600110F RID: 4367 RVA: 0x0004FDD0 File Offset: 0x0004DFD0
		public virtual string RecordFileName()
		{
			string str = "";
			return "/table." + this.TableTag + str + ".csv";
		}

		// Token: 0x06001110 RID: 4368 RVA: 0x00002A20 File Offset: 0x00000C20
		public virtual void PrepareAssets()
		{
		}

		// Token: 0x06001111 RID: 4369 RVA: 0x0004FE00 File Offset: 0x0004E000
		public virtual void CheckAssets(AssetDataBase assetData)
		{
			foreach (RecordRule recordRule in this.recordRules)
			{
				try
				{
					bool flag = true;
					bool flag2 = !this.dicEnableRules.TryGetValue(recordRule, out flag);
					if (flag2)
					{
						flag = true;
					}
					bool flag3 = flag;
					if (flag3)
					{
						try
						{
							recordRule.Check(assetData);
						}
						catch (Exception ex)
						{
							Debug.LogError(string.Concat(new string[]
							{
								string.Format("Record: {0}; Rule: {1}", assetData.GetInfo(), recordRule.GetType().Name),
								"\n",
								(ex.GetType() == null) ? null : ex.GetType().ToString(),
								"\n",
								ex.Message,
								"\n",
								ex.StackTrace
							}));
						}
					}
				}
				catch (Exception ex2)
				{
					Debug.LogError(string.Concat(new string[]
					{
						string.Format("Record: {0}; Rule: {1}", assetData.GetInfo(), recordRule.GetType().Name),
						"\n",
						(ex2.GetType() == null) ? null : ex2.GetType().ToString(),
						"\n",
						ex2.Message,
						"\n",
						ex2.StackTrace
					}));
				}
			}
		}

		// Token: 0x06001112 RID: 4370 RVA: 0x0004FFD4 File Offset: 0x0004E1D4
		protected virtual void ActiveAllRecord()
		{
			foreach (RecordRule key in this.recordRules)
			{
				this.dicEnableRules[key] = true;
			}
		}

		// Token: 0x06001113 RID: 4371 RVA: 0x00050034 File Offset: 0x0004E234
		protected virtual void ValidateAsset(AssetDataBase assetData)
		{
			foreach (RecordRule recordRule in this.recordRules)
			{
				try
				{
					bool flag = true;
					bool flag2 = !this.dicEnableRules.TryGetValue(recordRule, out flag);
					if (flag2)
					{
						flag = true;
					}
					bool flag3 = flag;
					if (flag3)
					{
						try
						{
							recordRule.Check(assetData);
						}
						catch (Exception ex)
						{
							ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, ScanLog.Stage.s3, string.Format("Record: {0}; Rule: {1}", assetData.GetInfo(), recordRule.GetType().Name), string.Concat(new string[]
							{
								"\n",
								(ex.GetType() == null) ? null : ex.GetType().ToString(),
								"\n",
								ex.Message,
								"\n",
								ex.StackTrace
							}));
						}
					}
				}
				catch (Exception ex2)
				{
					ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, ScanLog.Stage.s3, string.Format("Record: {0}; Rule: {1}", assetData.GetInfo(), recordRule.GetType().Name), string.Concat(new string[]
					{
						"\n",
						(ex2.GetType() == null) ? null : ex2.GetType().ToString(),
						"\n",
						ex2.Message,
						"\n",
						ex2.StackTrace
					}));
					throw;
				}
			}
		}

		// Token: 0x06001114 RID: 4372 RVA: 0x00050214 File Offset: 0x0004E414
		protected virtual bool CheckAssetPathRules(string assetPath)
		{
			bool flag = string.IsNullOrEmpty(assetPath);
			if (flag)
			{
				throw new Exception("Asset path is null.");
			}
			bool result = false;
			foreach (RecordRule recordRule in this.recordRules)
			{
				bool separateDirSettingsEnabled = recordRule.SeparateDirSettingsEnabled;
				if (separateDirSettingsEnabled)
				{
					bool flag2 = recordRule.DirTest(assetPath) && !recordRule.WhiteListTest(assetPath);
					this.dicEnableRules[recordRule] = flag2;
					bool flag3 = flag2;
					if (flag3)
					{
						result = true;
					}
				}
				else
				{
					bool flag4 = AssetPathManager.Instance.DirTest(assetPath) && !recordRule.WhiteListTest(assetPath);
					this.dicEnableRules[recordRule] = flag4;
					bool flag5 = flag4;
					if (flag5)
					{
						result = true;
					}
				}
			}
			return result;
		}

		// Token: 0x06001115 RID: 4373 RVA: 0x00050308 File Offset: 0x0004E508
		protected virtual bool EnableRule(string assetPath)
		{
			bool flag = string.IsNullOrEmpty(assetPath);
			if (flag)
			{
				throw new Exception("Asset path is null.");
			}
			bool result = false;
			foreach (RecordRule recordRule in this.recordRules)
			{
				bool separateDirSettingsEnabled = recordRule.SeparateDirSettingsEnabled;
				bool flag2;
				if (separateDirSettingsEnabled)
				{
					flag2 = (recordRule.DirTest(assetPath) && !recordRule.WhiteListTest(assetPath));
				}
				else
				{
					flag2 = (AssetPathManager.Instance.DirTest(assetPath) && !recordRule.WhiteListTest(assetPath));
				}
				this.dicEnableRules[recordRule] = flag2;
				bool flag3 = flag2;
				if (flag3)
				{
					result = true;
				}
			}
			return result;
		}

		// Token: 0x17000381 RID: 897
		// (get) Token: 0x06001116 RID: 4374 RVA: 0x000503DC File Offset: 0x0004E5DC
		public virtual string TableTag
		{
			get
			{
				return "default";
			}
		}

		// Token: 0x040009F3 RID: 2547
		public List<T> datas;

		// Token: 0x040009F4 RID: 2548
		protected List<RecordRule> recordRules;

		// Token: 0x040009F5 RID: 2549
		protected Dictionary<RecordRule, bool> dicEnableRules;
	}
}
